use std::{thread, time::Duration};

use ipc_channel::{ipc, ipc::IpcOneShotServer};

fn main() {
    let (server, token) =
        IpcOneShotServer::<String>::new().expect("Failed to create IPC one-shot server");

    println!("token ={}", token);

    let mut child = std::process::Command::new("demo_ipc_client.exe")
        .arg(&token)
        .spawn()
        .expect("启动子进程失败");

    let (rx, _) = server.accept().expect("Failed to accept connection");

    println!("[主进程] 收到响应通道");

    for _ in 0..10 {
        if let Ok(msg) = rx.recv() {
            println!("[主进程] 收到: {}", msg);
        }
    }

    thread::sleep(std::time::Duration::from_secs(3));

    println!("[主进程] 退出");

    let _ = std::process::Command::new("cmd")
        .args(&["/c", "pause"])
        .status();
}
